package com.taptech.doufu.ui.view.topicview.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.adapter.DFMemberAdapter;
import com.taptech.doufu.ui.adapter.DfhomeNovelAdapter3;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.novel.GetVipTicketGsonBean;
import com.taptech.doufu.bean.novel.VipMixWorksBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.ui.view.topicview.UmengEventUtil;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class VipNomalGridAdapter extends RecyclerView.Adapter<VipNomalGridAdapter.ViewHolder>{

    private List<VipMixWorksBean> list;
    private Context context;
    private int type;
    private String sexType;
    private String cardTitle;
    private EventBean eventBean;
    private int picWidth;
    private int picHeight;
    public VipNomalGridAdapter(Context context, List<VipMixWorksBean> list,int type,String sexType,String cardTitle,EventBean eventBean){
        this.list = list;
        this.context = context;
        this.type = type;
        this.sexType = sexType;
        this.cardTitle = cardTitle;
        this.eventBean = eventBean;
        picHeight = ChoiceUtils.getHeight(context);
        picWidth = ChoiceUtils.getPicWidth(context);
    }

    public VipMixWorksBean getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }

    public void refrush(List<VipMixWorksBean> list) {
        if (list != null) {
            this.list = list;
        }else {
            this.list.clear();
        }
        notifyDataSetChanged();
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (type == DFMemberAdapter.TYPE_VERTICAL) {
            View view = LayoutInflater.from(context).inflate(R.layout.topic_vip_nomal_item, null);
            ViewHolder holder = new ViewHolder(view);
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.tvAuthorDesc = (TextView) view.findViewById(R.id.tvAuthorDesc);
            holder.tvDesc = (TextView) view.findViewById(R.id.tvDesc);
            holder.tvTag1 = (TextView) view.findViewById(R.id.tvTag1);
            holder.tvTag2 = (TextView) view.findViewById(R.id.tvTag2);
            holder.ivPic = (ImageView) view.findViewById(R.id.ivPic);
            holder.ivVip = (ImageView) view.findViewById(R.id.ivVip);
            holder.rivAuthorPic = (ImageView) view.findViewById(R.id.rivAuthorPic);
            holder.lay = view.findViewById(R.id.lay);
            holder.bottom_line = view.findViewById(R.id.bottom_line);
            return holder;
        }else if (type == DFMemberAdapter.TYPE_CHAT_NOVEL_ALL_FREE) {
            View view = LayoutInflater.from(context).inflate(R.layout.cn_topic_grid_item, null);
            ViewHolder holder = new ViewHolder(view);
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.ivPic = (ImageView) view.findViewById(R.id.ivPic);
            holder.ivVip = (ImageView) view.findViewById(R.id.ivVip);
            holder.lay = view.findViewById(R.id.lay);
            return holder;
        } else if (type == DFMemberAdapter.TYPE_HORIZONTAL) {
            View view = LayoutInflater.from(context).inflate(R.layout.vip_topic_grid_item, null);
            ViewHolder holder = new ViewHolder(view);
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.tvTag1 = (TextView) view.findViewById(R.id.tvTag1);
            holder.tvTag2 = (TextView) view.findViewById(R.id.tvTag2);
            holder.tvGetTicket = (TextView) view.findViewById(R.id.tvGetTicket);
            holder.ivPic = (ImageView) view.findViewById(R.id.ivPic);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ScreenUtil.dip2px(context,100), ScreenUtil.dip2px(context,134));
            params.rightMargin = ScreenUtil.dip2px(context, 8);
            holder.ivPic.setLayoutParams(params);
            holder.ivVip = (ImageView) view.findViewById(R.id.ivVip);
            holder.title.setMaxWidth(ScreenUtil.dip2px(context,100));
            holder.lay = view.findViewById(R.id.lay);
            return holder;
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.vip_topic_grid_item, null);
            ViewHolder holder = new ViewHolder(view);
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.tvTag1 = (TextView) view.findViewById(R.id.tvTag1);
            holder.tvTag2 = (TextView) view.findViewById(R.id.tvTag2);
            holder.tvGetTicket = (TextView) view.findViewById(R.id.tvGetTicket);
            holder.ivPic = (ImageView) view.findViewById(R.id.ivPic);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(picWidth, picHeight);
            holder.ivPic.setLayoutParams(params);
            holder.ivVip = (ImageView) view.findViewById(R.id.ivVip);
            holder.title.setMaxWidth(picWidth);
            holder.lay = view.findViewById(R.id.lay);
            return holder;
        }

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final VipMixWorksBean model = getItem(position);
        if (model != null) {
            if (type == DFMemberAdapter.TYPE_VERTICAL){
                if (!TextUtils.isEmpty(model.getDescription())) {
                    holder.tvDesc.setText(model.getDescription());
                    holder.tvDesc.setVisibility(View.VISIBLE);
                }else if (!TextUtils.isEmpty(model.getIntro())) {
                    holder.tvDesc.setText(model.getIntro());
                    holder.tvDesc.setVisibility(View.VISIBLE);
                }else{
                    holder.tvDesc.setVisibility(View.INVISIBLE);
                }
                if (model.getUser() != null) {
                    if (!TextUtils.isEmpty(model.getUser().getHead())) {
                        GlideUtil.displayCircleImage(holder.rivAuthorPic, model.getUser().getHead());
                        holder.rivAuthorPic.setVisibility(View.VISIBLE);
                    }else{
                        holder.rivAuthorPic.setVisibility(View.GONE);
                    }
                    if (!TextUtils.isEmpty(model.getUser().getNickname())) {
                        holder.tvAuthorDesc.setVisibility(View.VISIBLE);
                        holder.tvAuthorDesc.setText(model.getUser().getNickname());
                    }else{
                        holder.tvAuthorDesc.setVisibility(View.GONE);
                    }
                }
                if (position == getItemCount() - 1) {
                    holder.bottom_line.setVisibility(View.GONE);
                }else{
                    holder.bottom_line.setVisibility(View.VISIBLE);
                }
                setNomalNovel(model,holder);
            }else if (type == DFMemberAdapter.TYPE_CHAT_NOVEL_ALL_FREE) {
                setChatNovel(model,holder);
            }else{
                setGridNovel(model,holder);
            }
            changeDayModelBg(holder);
        }
    }


    private void setChatNovel(final VipMixWorksBean mixWorksBean, ViewHolder holder) {
        holder.title.setText(mixWorksBean.getTitle());
        if (!TextUtils.isEmpty(mixWorksBean.getCover())) {
            GlideUtil.displayRoundImage(context,holder.ivPic,mixWorksBean.getCover(),3,R.drawable.img_default_loading_l);
        }
        if (mixWorksBean.getMasterIcons() != null &&
                mixWorksBean.getMasterIcons().size() > 0 &&
                mixWorksBean.getMasterIcons().get(0) != null &&
                !TextUtils.isEmpty(mixWorksBean.getMasterIcons().get(0).getUrl())) {
            holder.ivVip.setVisibility(View.VISIBLE);
            GlideUtil.displayRoundImage(context,holder.ivVip,mixWorksBean.getMasterIcons().get(0).getUrl(),3,R.drawable.img_default_loading_l);
        }else{
            holder.ivVip.setVisibility(View.GONE);
        }
        holder.ivVip.setVisibility(View.GONE);
        holder.lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengEventUtil.addEvent(context,UmengEventName.VIP_CARD_CONTENT,eventBean);
                HashMap map = new HashMap();
                map.put("id", mixWorksBean.getId());
                String type = DiaoBaoSharedPreferences.getSharedPreferencesValueToString("themeType",WeMediaApplication.applicationContext,"");
                if (!TextUtils.isEmpty(type)) {
                    map.put("themeType", JSON.parse(type));
                }
                SimpleWeexActivity.startActivity(context,"vue/speakNovel/read/WWRead.js", map);
            }
        });

    }


    private void setNomalNovel(final VipMixWorksBean model, final ViewHolder holder) {

        holder.title.setText(model.getTitle());
        if (model.getIs_vip()== 1) {
            holder.ivVip.setVisibility(View.VISIBLE);
        } else {
            holder.ivVip.setVisibility(View.GONE);
        }
        holder.ivVip.setVisibility(View.GONE);

        String cover = model.getCover();
        List<VipMixWorksBean.TagBean> tags = null;

        if (type == DFMemberAdapter.TYPE_NOMAL_COMMIC_DISCOUNT || model.getObject_type() == 45) { //漫画
            cover = model.getImage();
            if (!TextUtils.isEmpty(model.getTag_list())) {
                String[] strings = model.getTag_list().split(" ");
                tags = new ArrayList<>();
                if (strings.length > 0) {
                    VipMixWorksBean.TagBean bean = new VipMixWorksBean.TagBean();
                    bean.setName(strings[0]);
                    tags.add(bean);
                }
                if (strings.length > 1) {
                    VipMixWorksBean.TagBean bean = new VipMixWorksBean.TagBean();
                    bean.setName(strings[1]);
                    tags.add(bean);
                }
            }
        }else{
            tags = model.getTags();
        }

        if (!TextUtils.isEmpty(cover)) {
            GlideUtil.displayRoundImage(context, holder.ivPic,cover,3,R.drawable.img_default_loading_l);
        }

        if (tags!=null){
            if (tags.size() > 0 && tags.get(0) != null && !TextUtils.isEmpty(tags.get(0).getName())) {
                holder.tvTag1.setVisibility(View.VISIBLE);
                holder.tvTag1.setText(tags.get(0).getName());
            }else{
                holder.tvTag1.setVisibility(View.GONE);
            }
            if (tags.size() > 1 && tags.get(1) != null && !TextUtils.isEmpty(tags.get(1).getName())) {
                holder.tvTag2.setText(tags.get(1).getName());
                holder.tvTag2.setVisibility(View.VISIBLE);
            }else{
                holder.tvTag2.setVisibility(View.GONE);
            }
        }

        holder.lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengEventUtil.addEvent(context,UmengEventName.VIP_CARD_CONTENT,eventBean);
                if(type == DFMemberAdapter.TYPE_NOMAL_COMMIC_DISCOUNT ||model.getObject_type() == 45){ //漫画
                    CartoonServices.enterCartoonDes(context,String.valueOf(model.getId()));
                }else{//小说
                    DfhomeNovelAdapter3.goToNovelDesActivity(context,String.valueOf(model.getId()),
                            String.valueOf(model.getObject_type()), model.getTitle(),sexType,cardTitle);
                }
            }
        });
        holder.tvTag1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DfhomeNovelAdapter3.goToTagActivity(context,holder.tvTag1.getText().toString(),"");
            }
        });
        holder.tvTag2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DfhomeNovelAdapter3.goToTagActivity(context,holder.tvTag2.getText().toString(),"");
            }
        });

    }



    private void setGridNovel(final VipMixWorksBean model, final ViewHolder holder) {
        if (model.getVipReadTicket() > 0 || model.isVipReadTicketReceived()) {
            holder.tvGetTicket.setVisibility(View.VISIBLE);
            if (model.isVipReadTicketReceived()) {  //已领取
                holder.tvGetTicket.setText("已领取");
                holder.tvGetTicket.setBackgroundResource(R.drawable.get_ticket_btn_bg_gray);
                holder.tvGetTicket.setTextColor(Color.parseColor("#999999"));
            }else{
                holder.tvGetTicket.setText("领取");
                if(WeMediaApplication.getInstance().isDayNightMode_Night){
                    holder.tvGetTicket.setTextColor(Color.parseColor("#7f3037"));
                    holder.tvGetTicket.setBackgroundResource(R.drawable.get_ticket_btn_bg_night);
                }else {
                    holder.tvGetTicket.setTextColor(Color.parseColor("#ff6e70"));
                    holder.tvGetTicket.setBackgroundResource(R.drawable.get_ticket_btn_bg);
                }
            }
        }else{
            holder.tvGetTicket.setVisibility(View.GONE);
        }
        holder.tvGetTicket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!model.isVipReadTicketReceived()) {
                    if (AccountService.getInstance().isLogin()) {
                        getTicket(model);
                    } else {
                        AccountService.getInstance().jumpToLogin();
                    }
                }
            }
        });
        setNomalNovel(model,holder);

    }


    private void getTicket(final VipMixWorksBean model) {
        ApiClient.getInstance().setUseCache(false).getService()
                .getVipTicket(model.getId(),model.getObject_type())
                .compose(RxJavaHelper.<GetVipTicketGsonBean>observeOnMainThread((Activity) context))
                .subscribe(new BaseSubscriber<GetVipTicketGsonBean>(){
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Toast.makeText(context, "领取失败", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onNext(GetVipTicketGsonBean bean) {
                        super.onNext(bean);
                        if (bean!=null) {
                            if (bean.getData() != null && bean.getData().isStatus()) {
                                Toast.makeText(context, "领取成功", Toast.LENGTH_LONG).show();
                                model.setVipReadTicketReceived(true);
                                notifyDataSetChanged();
                            }else{
                                if (!TextUtils.isEmpty(bean.getUser_msg())) {
                                    Toast.makeText(context, bean.getUser_msg(), Toast.LENGTH_LONG).show();
                                    if (bean.getStatus() == 30805) {  //已经领取
                                        model.setVipReadTicketReceived(true);
                                        notifyDataSetChanged();
                                    }
                                }else{
                                    Toast.makeText(context, "领取失败", Toast.LENGTH_LONG).show();
                                }
                            }

                        }else{
                            Toast.makeText(context, "领取失败", Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView ivPic;
        ImageView ivVip;
        View lay,bottom_line;
        TextView tvTag1,tvTag2;
        TextView tvGetTicket;
        TextView tvDesc;
        ImageView rivAuthorPic;
        TextView tvAuthorDesc;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_color_22));
        viewHolder.ivPic.setAlpha(1.0f);
        viewHolder.ivVip.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
        viewHolder.ivVip.setAlpha(Constant.imagViewAlpha);
    }
}
